package cn.mediatek.server.module;

import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.ModelAttribute;


/**
 * @author geekzhang
 * @version 1.0
 * @date 2020/9/30 下午 1:12
 * @description
 */
public class BaseController {
    private static ThreadLocal<ServletRequest> REQUESTTHREADLOCAL = new ThreadLocal<>();

    private static ThreadLocal<ServletResponse> RESPONSETHREADLOCAL = new ThreadLocal<>();

    /**
     * 初始化request  response
     *
     * @param request
     * @param response
     */
    @ModelAttribute
    public void initHttpServlet(HttpServletRequest request, HttpServletResponse response) {
        REQUESTTHREADLOCAL.set(request);
        RESPONSETHREADLOCAL.set(response);
    }

    /**
     * 获取线程安全的request
     *
     * @return
     */
    public HttpServletRequest request() {
        return (HttpServletRequest) REQUESTTHREADLOCAL.get();
    }

    /**
     * 获取线程安全的response
     *
     * @return
     */
    public HttpServletResponse response() {
        return (HttpServletResponse) RESPONSETHREADLOCAL.get();
    }

}
